import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.Serializable;

import javax.swing.*;

/**
 * represents a town
 * located in the middle of the Map
 * @author Sammy Warrayat
 *
 */
public class Town implements Serializable 
{
	/*
	 * store is the store inside town
	 * land is the land office inside town
	 * pub is the pub inside town
	 */
	private Store store;
	private LandOffice land;
	private Pub pub;
	
	/**
	 * creates an instance of a town
	 */
	public Town(){
		store = new Store();
		land = new LandOffice();
		pub = new Pub();
	}
	
	/**
	 * gets the store in the town
	 * @return the town store
	 */
	public Store getStore()
	{
		return store;
	}
	
	/**
	 * gets the land office in the town
	 * @return the town land office
	 */
	public LandOffice getLandOffice()
	{
		return land;
	}
	
	/**
	 * gets the pub in the town
	 * @return the town pub
	 */
	public Pub getPub()
	{
		return pub;
	}
	
	/**
	 * creates the town panel
	 * @return the town panel
	 * @throws IOException 
	 */
	public JPanel createTown() throws IOException
	{
		//totalRep is a JPanel will be returned by createTown()
		final JPanel totalRep = new JPanel(new BorderLayout());
		GameModel.getInstance().setLabel("<html><center>Welcome to the town!</center></html>");
		totalRep.add(GameModel.getInstance().getLabel(), BorderLayout.NORTH);
		//townRep is the JPanel that represents the town
		//storeButton is the button that allows you to go to the store
		final JPanel townRep = new JPanel(new GridLayout(2,2));
		PicButton storeButton = new PicButton(getClass().getResource("Images/store.png"));
		storeButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				townRep.setVisible(false);
				StorePanel sp = new StorePanel(store);
				JPanel sPan = sp.generatePanel();
				totalRep.add(sPan);
				sPan.setVisible(true);
				}});
		townRep.add(storeButton);
		//pub is the button that allows you to go to the pub
		PicButton pub = new PicButton(getClass().getResource("Images/pub.png"));
		pub.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				GameModel.getInstance().getTurn().visitPub();
			}});
		townRep.add(pub);
		PicButton lo = new PicButton(getClass().getResource("Images/landoffice.png"));
		lo.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				townRep.setVisible(false);
				LandOfficePanel lp = new LandOfficePanel(land);
				JPanel lPan = lp.generatePanel();
				totalRep.add(lPan);
				lPan.setVisible(true);
			}});
		townRep.add(lo);
		//returnMap allows the player to go back tot he Map
		PicButton returnMap = new PicButton(getClass().getResource("Images/returnmap.png"));
		returnMap.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				townRep.setVisible(false);
				JPanel map;
				try {
					map = GameModel.getInstance().getMap().generatePanel();
					totalRep.add(map);
					map.setVisible(true);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}});
		townRep.add(returnMap);
		totalRep.add(townRep, BorderLayout.CENTER);
        return totalRep;
	}
}
